<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\Api\model\Orders;
use app\api\service\Swoole;
use think\Request;

class OrderController
{
    /**
     * 订单生命周期
     * 订单状态
     */
    public static function order(){
        $redis = new Redis();
        $redis->connect('127.0.0.1',6379);
        $server=new Swoole();
        $server->start();
        //修改未支付的支付状态
        swoole_timer_tick(1000*60,function ()use($redis){
            //修改未支付的支付状态
            $orders=Orders::where('status',0)->select()->toArray();
            foreach ($orders as $v){
                if ($v['create_time']+15*3600<time()){
                    Orders::where('status',$v['id'])->update(['status'=>10]);
                }
            }
        });
    }
    /**
     * 订单支付状态
     */
    public static function orderPay(){

    }
    /**
     * 订单状态修该
     */
    public static function orderStatus(Request $request){
        $order_no=$request->param('order_no');
        $status=$request->param('status');
        $order=Orders::where('order_no',$order_no)->find();
        //快递状态
//        1-已支付 2-揽件中 3-已发货 4-已完成 5-退款中 6-已退款 7-支付失败 8-待核销  9-已核销
        switch ($status){
            case $status==2;
                $res=Orders::where('order_no',$order_no)->update(['status'=>2]);
                break;
            case $status==3;
                $res=Orders::where('order_no',$order_no)->update(['status'=>3]);
                break;
            case $status==4;
                $res=Orders::where('order_no',$order_no)->update(['status'=>4]);
                break;
            case $status==1;
                $res=Orders::where('order_no',$order_no)->update(['status'=>1]);
                break;
            case $status==5;
                $res=Orders::where('order_no',$order_no)->update(['status'=>5]);
                break;
//           case $status==6;
//               $res=Orders::where('order_no',$order_no)->update(['status'=>6]);
//               break;
//           case $status==7;
//               $res= Orders::where('order_no',$order_no)->update(['status'=>7]);
//               break;
            case $status==8;
                $res=Orders::where('order_no',$order_no)->update(['status'=>8]);
                break;
            case $status==9;
                $res=Orders::where('order_no',$order_no)->update(['status'=>9]);
                break;
        }
        if ($res){
            return json_encode(['code'=>1001,'msg'=>'状态修改成功','data'=>true]);
        }else{
            return json_encode(['code'=>5001,'msg'=>'状态修改失败','data'=>false]);
        }
    }

}
